package com.xuecheng.system.controller;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.Company;

import com.xuecheng.system.service.CompanyService;
import com.xuecheng.system.web.exception.BusinessException;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;


public class CompanyController {
    @Autowired
    CompanyService companyService;
    @GetMapping("/mine")
    public ResponseResult mine(@RequestHeader("Authorization") String token) {
        Boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
        Claims claims = JwtUtils.parserToken(token).getBody();
        Long companyId = claims.get("companyId", Long.class);
        Company company = companyService.getById(companyId);
        CompanyVo vo = BeanHelper.copyProperties(company, CompanyVo.class);
        return ResponseResult.okResult(vo);

    }
}
